﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using logistics;
using Logistics.Win;
using Microsoft.Windows.Controls.Ribbon;
using Logistics.Configuration;

namespace Logistics
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : RibbonWindow
    {
        private UserIdentity m_CurrentUser;
        RecordEditWindow m_WinEditor = null;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            m_CurrentUser = GlobalObjects.CurrentUser;
            if (m_CurrentUser == null)
            {
                MessageBox.Show("内部错误，无法获取当前登录用户信息,程序将退出", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                Application.Current.Shutdown();
                return;
            }
            this.txtUserName.Text = m_CurrentUser.RealName;
        }

        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (m_CurrentUser != null)
            {
                MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(this, "确定要退出吗？", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
                //MessageBoxResult result = MessageBox.Show("确定要退出吗？", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (result == MessageBoxResult.Yes)
                {
                   
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        #region 菜单按钮响应
        private void MenuItemNew_Click(object sender, RoutedEventArgs e)
        {
            BtnNewRecord_Click(sender,e);
        }
        private void MenuItemSetting_Click(object sender, RoutedEventArgs e)
        {
            BtnSysSetting_Click(sender, e);
        }
        private void MenuItemAbout_Click(object sender, RoutedEventArgs e)
        {
            BtnAbout_Click(sender,e);
        }
        private void MenuItemExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        private void BtnNewRecord_Click(object sender, RoutedEventArgs e)
        {
            if (m_WinEditor == null)
            {
                m_WinEditor = new RecordEditWindow();
                m_WinEditor.DataChanged += (o, arg) =>
                {
                    MyGrid.ReloadData();
                };
                m_WinEditor.Closed += (o, arg) =>
                {
                    m_WinEditor = null;
                    MyGrid.ReloadData();
                };
            }
            m_WinEditor.Show();
            m_WinEditor.Activate();
        }

        private void BtnEditRecord_Click(object sender, RoutedEventArgs e)
        {
            String selectedItem = this.MyGrid.SelectedItem;
            if (String.IsNullOrEmpty(selectedItem))
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("请选择需要编辑的单据!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                return;
            }
            RecordEditWindow win = new RecordEditWindow(selectedItem);
            win.DataChanged += (o, arg) =>
            {
                MyGrid.ReloadData();
            };
            win.Closed += (o, arg) =>
            {
                MyGrid.ReloadData();
            };
            win.Show();
        }

        private void BtnViewRecord_Click(object sender, RoutedEventArgs e)
        {
            MyGrid.ReloadData();
        }

        private void BtnDataUpload_Click(object sender, RoutedEventArgs e)
        {
            DataUploadWin win = new DataUploadWin();
            win.ShowDialog();
            MyGrid.ReloadData();
        }

        private void BtnDataSynchronize_Click(object sender, RoutedEventArgs e)
        {
            DataDownloadWin win = new DataDownloadWin() { OnlyForCustomerDownload = true};
            win.AutoClose = true;//自动关闭
            win.ShowDialog();
        }

        private void BtnHelp_Click(object sender, RoutedEventArgs e)
        {
            String helpUrl = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"帮助.chm");
            if (File.Exists(helpUrl))
            {
                Process.Start(helpUrl);
            }
        }
        private void BtnAbout_Click(object sender, RoutedEventArgs e)
        {
            HelpInfor aboutfrm = new HelpInfor();
            aboutfrm.ShowDialog();
        }
        private void BtnSysLogs_Click(object sender, RoutedEventArgs e)
        {
            WinLogs logsWin = new WinLogs();
            logsWin.ShowDialog();
        }

        private void BtnSysSetting_Click(object sender, RoutedEventArgs e)
        {
            WinSettings sytemSetting = new WinSettings();
            sytemSetting.ShowDialog();
        }

        private void BtnCheckUpdate_Click(object sender, RoutedEventArgs e)
        {
            String updaterPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AppUpgrader.exe");
            String ver = GlobalSetting.CurrentVersion;
            String url = GlobalSetting.UpdateSourceURL;
            String args= "";
            if(!String.IsNullOrWhiteSpace(ver) && !String.IsNullOrWhiteSpace(url))
                args = String.Format("-v {0} -url {1}", ver, url);
            Process.Start(updaterPath, args);
        }
        #endregion
    }
}
